<?php
use yii\helpers\Url;

$web = Url::base();

?>
<script type="application/javascript">
    $(function ($) {

        var record =<?=$record?>;
        $("#alert1").css("display", "none");
        $("#alert2").css("display", "none");
        $("#alert3").css("display", "none");
        // $("#addUser").modal('show');

        $("#userId").val(record.id);


        $("#userName").val(record.name);
        $("#userPhone").val(record.phone);
        if (record.is_enabled == 1) {
            $("#isEnabled").prop("checked", true);
        } else {
            $("#isEnabled").prop("checked", false);
        }
        $("#remark").val(record.remark);


    });

    //新增修改会员确认
    function addConfirm() {
        var data = {};
        var checkPhone = /^1\d{10}$/;
        data.id = $("#userId").val();

        data.name = $("#userName").val();
        data.institution_code = $("#select3").val();
        data.phone = $("#userPhone").val();
        data.remark = $("#remark").val();
        if ($("#isEnabled").is(':checked') == true) {
            data.isEnabled = 1;
        } else {
            data.isEnabled = 0;
        }

        if (data.institution_code == 0) {
            $("#alert3").css("display", "block");
        }
        else if (data.name == '') {
            $("#alert1").css("display", "block");
        }
        else if (data.phone == '' || !checkPhone.test(data.phone)) {
            $("#alert2").css("display", "block");
        } else {
            loading.show();
            requestJson("add-user-handler", data, true, function (re) {
                loading.hide();
                if (re.status == 0) {
                    alert('操作成功！');
                    isRefresh = data.id;
                    refresh();
                }
                else if (re.status == 9) {
                    alert('该手机号码已被注册过！');
                } else {
                    alert('操作失败！');
                }
                //$("#institutionModal3").modal('show');


            });
        }
    }
</script>

<div class="modal-body">
    <form class="form-horizontal adminex-form">
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red">*</span>会员姓名</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="userName" placeholder="">
                <input class="hidden_id" id="userId" type="hidden" value="0"/>
                <label id='alert1' style="display: none;"><span style="color: red">会员姓名不允许为空</span></label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">所属机构</label>
            <div class="col-sm-9">
                <?= app\widgets\InstitutionHierarchyAutocompleteWidget::widget(['id' => 'select3', 'currentInstitutionCode' => $record['institution_code'], 'defaultText' => '----请选择机构----']) ?>
                <label id='alert3' style="display: none;"><span style="color: red">请选择一个机构</span></label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><span style="color: red">*</span>电话</label>
            <div class="col-sm-9">
                <input type="text" class="form-control" id="userPhone" placeholder="">
                <label id='alert2' style="display: none;"><span
                        style="color: red">手机号码不正确或为为空！</span></label>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-10">
                <label class="checkbox-inline">
                    <input type="checkbox" id="isEnabled" value="option4" checked="true"><span
                        class="btn-clipboard popovers">是否启用</span>
                </label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注</label>
            <div class="col-sm-9">
                <textarea rows="6" class="form-control" id="remark"></textarea>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button class="btn btn-default" data-dismiss="modal">关闭</button>
    <button class="btn btn-primary" onclick="addConfirm()">保存</button>
</div>

